import { Component } from "@angular/core"
import { AlertController} from 'ionic-angular';

import { AppConfig } from '../../../../app/app.config';

import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';
@Component({
    selector: 'page-set',
    templateUrl: 'cust.html',
})

export class CustPage{
    form = {
        content:'',
        type:2
    }
    token:any;
    constructor(
        private alertCtrl:AlertController,
        private http:HttpClient,
        private storage: NativeStorage,
        private translate:TranslateService
    ) {
        this.storage.getItem('token').then(token=>{
            this.token = token   
        })
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.storage.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    goCust(){
        if(this.form.content.length<=500){
            this.http.post(AppConfig.getUrl()+'feedback/add?token='+this.token, AppConfig.urlEcode(this.form),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( res => {
                if(res['code']===101){
                    this.alertb('success','Submit successful!');
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
        }else{
            this.alertb('Error!','The number of words cannot exceed 500!');
        }
    }
}